package com.woniu.shops.service.imp;

import com.woniu.common.CustomException.UserException;
import com.woniu.common.pojo.Flower;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.Type;
import com.woniu.shops.mapper.CommodityMapper;
import com.woniu.shops.mapper.TypeMapper;
import com.woniu.shops.service.TypeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

@Service
public class TypeServiceImp implements TypeService {
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CommodityMapper commodityMapper;

    @Override
    public List<Type> getTypeList(Shops shops) {
        if(shops!=null){
            List<Type> typeList = (List<Type>) redisTemplate.opsForValue().get("types");
            if (typeList == null || typeList.isEmpty() || typeList.size() == 0) {
                List<Type> typeList1 = typeMapper.getTypeList(shops.getSid());
                if (typeList1 != null) {
                    redisTemplate.opsForValue().set("types", typeList1);
                    return typeList1;
                }
                throw new UserException("不好意思,类型空了");
            }
            return typeList;
        }
        throw new UserException("不好意思,商铺不存在了");
    }

    @Override
    public void deleteType(Type types) {
        if (types != null) {
            List<Flower> flowerList= commodityMapper.findTypes(types.getTid());
            if(flowerList.size() == 0 || flowerList == null){
                Integer i = typeMapper.deleteType(types);
                if (i > 0) {
                    redisTemplate.delete("types");
                    return ;
                }
                throw new UserException("不好意思,数据不存在,删除失败");
            }
            throw new UserException("不好意思,该类型在使用呢");
        }
        throw new UserException("不好意思,类型空了");
    }

    @Override
    public void updateType(Type types) {
        if (types != null) {
            List<Type> typeList=typeMapper.getSameType(types);
            if(typeList.size() == 0 || typeList == null){
                Integer i = typeMapper.updateType(types);
                if (i > 0) {
                    redisTemplate.delete("types");
                    return ;
                }
                throw new UserException("不好意思,数据不存在,修改失败");
            }
            throw new UserException("不好意思,该名称在使用呢");
        }
        throw new UserException("不好意思,类型空了");
    }

    @Override
    public void addTypes(Type types) {
        if (types != null) {
            List<Type> typeList=typeMapper.getSameNameType(types);
            if(typeList.size() == 0 || typeList == null){
                Integer i = typeMapper.addTypes(types);
                if (i > 0) {
                    redisTemplate.delete("types");
                    return ;
                }
                throw new UserException("不好意思,添加失败");
            }
            throw new UserException("不好意思,该名称在使用呢");
        }
        throw new UserException("不好意思,类型空了");
    }
}
